package gov.fgn.sijyp.general.util;

import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("emptyStringValidator")
public class EmptyStringValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		String strValue = (String) value;
		if (strValue == null || strValue.trim().length() == 0) {
			FacesMessage message = new FacesMessage();
			ResourceBundle bundle = context.getApplication()
					.evaluateExpressionGet(context, "#{msgVal}",
							ResourceBundle.class);
			String msg = bundle.getString("constraint.not_null");
			message.setDetail(msg);
			message.setSummary(msg);
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
	}

}
